const mongoose = require('mongoose')

/**
 * 学历指导模型
 * - educationId: 学历指导记录ID
 * - isSuccessCase: 是否成功案例（1:是, 0:否）
 * - admissionDate: 录取时间（YYYY-MM-DD格式）
 * - files: 关联图片文件
 * - schoolName: 学校名称
 * - schoolCountry: 学校所在国家
 * - schoolCity: 学校所在城市
 * - upgradeType: 升学类型 1:专升本 2:专升硕 3:本升硕 4:硕升博 5:幼升小 6:小升初 7:初升高 8:高升专 9:高升本
 * - studentName: 学生姓名
 * - isDomestic: 是否国内学校（1:国内, 0:国外）
 */
const EducationSchema = new mongoose.Schema(
  {
    educationId: {
      type: String,
      required: true,
      default: function () {
        return this._id.toString()
      }
    },
    isSuccessCase: {
      type: Number,
      required: true,
      default: 0
    },
    admissionDate: {
      type: Date,
      required: true,
      get: v => v.toISOString().split('T')[0] // 格式化输出为YYYY-MM-DD
    },
    year: {
      type: Number,
      required: true,
      default: function () {
        const dateObj = this.admissionDate instanceof Date ? this.admissionDate : new Date(this.admissionDate)
        return dateObj.getUTCFullYear()
      }
    },
    files: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'File',
      required: true
    },
    schoolName: {
      type: String,
      required: true,
      maxlength: 100
    },
    schoolCountry: {
      type: String,
      required: true,
      default: '中国'
    },
    schoolCity: {
      type: String,
      required: true,
      maxlength: 50
    },
    upgradeType: {
      type: Number,
      required: true,
      enum: [1, 2, 3, 4, 5, 6, 7, 8, 9] // 限定可选类型
    },
    studentName: {
      type: String,
      required: true,
      maxlength: 30
    },
    createdAt: {
      type: Date,
      default: Date.now
    },
    isDomestic: {
      type: Number,
      default: function () {
        return this.schoolCountry === '中国' ? 1 : 0
      }
    }
  },
  { timestamps: true }
)

EducationSchema.set('toJSON', { getters: true })
EducationSchema.index({ schoolCountry: 1, isSuccessCase: 1, year: 1, admissionDate: 1 })

module.exports = mongoose.model('Education', EducationSchema)
